package common

import (
	"context"
	"errors"
	"github.com/gorhill/cronexpr"
	"time"
)

const (
	LOGID           = "__LOG_ID__"
	TEST_DATE       = "__TEST_DATE__"
	CODE_ERR        = -1
	CODE_OK         = 0
	ETCD_JOBS_DIR   = "/cron2/jobs/"
	ETCD_KILL_DIR   = "/cron2/kill/"
	ETCD_WORKER_DIR = "/cron2/worker/"
	ETCD_JOBS_LOCK  = "/cron2/lock/"

	JOB_TYPE_PUT  = 1
	JOB_TYPE_DEL  = 2
	JOB_TYPE_KILL = 3
)

var (
	ERR_LOCKED = errors.New("锁被占用")
)

type Job struct {
	Name     string               `json:"name"`
	Command  string               `json:"command"`
	CronExpr string               `json:"cronExpr"`
	Expr     *cronexpr.Expression `json:"-"`
	Type     int                  `json:"-"` // 1-put 2-delete
	NextTime time.Time            `json:"-"`
	Ctx      context.Context      `json:"-"`
	Cancel   context.CancelFunc   `json:"-"`
}

type Response struct {
	Errno int
	Msg   string
	Data  interface{}
}

type JobLog struct {
	JobName      string `json:"jobName" bson:"jobName"`           // 任务名字
	Command      string `json:"command" bson:"command"`           // 脚本命令
	Err          string `json:"err" bson:"err"`                   // 错误原因
	Output       string `json:"output" bson:"output"`             // 脚本输出
	PlanTime     int64  `json:"planTime" bson:"planTime"`         // 计划开始时间
	ScheduleTime int64  `json:"scheduleTime" bson:"scheduleTime"` // 实际调度时间
	StartTime    int64  `json:"startTime" bson:"startTime"`       // 任务执行开始时间
	EndTime      int64  `json:"endTime" bson:"endTime"`           // 任务执行结束时间
}
type ManyLogs struct {
	Logs []interface{}
}

type ExecResult struct {
	Job       *Job
	Err       error
	Output    []byte
	StartTime time.Time
	EndTime   time.Time
}

type LogRes struct {
	LogArr []*JobLog `json:"list"`
	Count  int64 `json:"count"`
}
